﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using SalesPortal.Web.Models;
using System.Globalization;

namespace SalesPortal.Web.Controllers
{
    public class EmployeeController : Controller
    {
        private SalesPortalEntities db = new SalesPortalEntities();
        private const int PAGE_SIZE = 25;

        // GET: Employee
        public async Task<ActionResult> SPA()
        {
            return View(new EmployeeListingViewModel
            {
                Filter = new EmployeeFilter(),
                EmployeeList = await db.Employees.OrderBy(r => r.LegalName).Skip(25).Take(25).ToListAsync(),
                TablePager = new TablePagination(db.Employees.OrderBy(r => r.LegalName).Count(), 25, 1)
            });
        }

        // GET: Employee
        public async Task<ActionResult> Index(int? p, EmployeeFilter filter)
        {
            int pageSize = 0;

            if (p.HasValue)
                pageSize = p.Value * 25;

            return View(new EmployeeListingViewModel
            {
                Filter = new EmployeeFilter(),
                EmployeeList = await db.Employees.OrderBy(r => r.LegalName).Skip(pageSize).Take(25).ToListAsync(),
                TablePager = new TablePagination(db.Employees.OrderBy(r => r.LegalName).Count(), 25, p)
            });
        }

        [HttpPost]
        public async Task<ActionResult> Index(EmployeeFilter filter, int? p)
        {
            IQueryable<Employee> query = db.Employees;

            if (filter.LegalName.IsDefined())
            {
                string filter_LegalName = filter.LegalName.Trim();
                query = query.Where(r => r.LegalName.Contains(filter_LegalName));
            }

            if (filter.StartDateOfBirth.IsDefined())
            {
                DateTime filter_StartDateOfBirth = filter.StartDateOfBirth.Value;
                query = query.Where(r => r.DateOfBirth >= filter_StartDateOfBirth);
            }

            if (filter.EndDateOfBirth.IsDefined())
            {
                DateTime filter_EndDateOfBirth = filter.EndDateOfBirth.Value;
                query = query.Where(r => r.DateOfBirth < filter_EndDateOfBirth);
            }

            if (filter.StartSalary.IsDefined())
            {
                Decimal filter_StartSalary = filter.StartSalary.Value;
                query = query.Where(r => r.Salary >= filter_StartSalary);
            }

            if (filter.EndSalary.IsDefined())
            {
                Decimal filter_EndSalary = filter.EndSalary.Value;
                query = query.Where(r => r.Salary < filter_EndSalary);
            }

            if (filter.Status.IsDefined())
            {
                byte filter_Status = filter.Status.Value;
                query = query.Where(r => r.Status == filter_Status);
            }

            return View(new EmployeeListingViewModel
            {
                Filter = filter,
                EmployeeList = await query.OrderBy(r => r.LegalName).ToListAsync(),
                TablePager = new TablePagination(query.OrderBy(r => r.LegalName).Count(), 25, p)
            });
        }

        // GET: Employee/Details/5
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Employee employee = await db.Employees.FindAsync(id);
            if (employee == null)
            {
                return HttpNotFound();
            }
            return View(employee);
        }

        // GET: Employee/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Employee/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "Id,LegalName,DateOfBirth,Salary,Status,FirstName,LastName")] Employee employee)
        {
            if (ModelState.IsValid)
            {
                db.Employees.Add(employee);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            return View(employee);
        }

        // GET: Employee/Edit/5
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Employee employee = await db.Employees.FindAsync(id);
            if (employee == null)
            {
                return HttpNotFound();
            }
            return View(employee);
        }

        // POST: Employee/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "Id,LegalName,DateOfBirth,Salary,Status,FirstName,LastName")] Employee employee)
        {
            if (ModelState.IsValid)
            {
                db.Entry(employee).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            return View(employee);
        }

        // GET: Employee/Delete/5
        public async Task<ActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Employee employee = await db.Employees.FindAsync(id);
            if (employee == null)
            {
                return HttpNotFound();
            }
            return View(employee);
        }

        // POST: Employee/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(int id)
        {
            Employee employee = await db.Employees.FindAsync(id);
            db.Employees.Remove(employee);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
